package com.acme.chat.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import com.acme.chat.client.ChatClient;
import com.acme.chat.client.ClientListener;
import com.acme.chat.message.ChatMessage;
import com.acme.chat.message.Message;
import com.acme.chat.message.MessageType;

public class TcpProxyClient implements ChatClient, Runnable {

    private Socket socket;
    private String room;
    private ObjectInputStream sInput;
    private ObjectOutputStream sOutput;
    private String name;

    private volatile boolean keepGoing = true;

    private Server server;

    public TcpProxyClient(Socket socket, Server server) {
        this.socket = socket;
        this.server = server;

        try {
            sOutput = new ObjectOutputStream(socket.getOutputStream());
            sInput = new ObjectInputStream(socket.getInputStream());

            // read userName and room
            name = (String) sInput.readObject();
            room = (String) sInput.readObject();

        } catch (IOException e) {
            e.printStackTrace();
            close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            close();
        }

        // try add client to the server
        boolean connectedToTheServer = server.addClient(this);
        if (!connectedToTheServer) {
            close();
        }
    }

    public String getRoom() {
        return room;
    }

    public String getName() {
        return name;
    }

    public boolean sendMessage(Message message) {
        return false;
    }

    public void disconnect() {
        try {
            sOutput.writeObject(new ChatMessage(MessageType.LOGOUT));
        } catch (IOException e) {
            e.printStackTrace();
        }
        keepGoing = false;
        close();
    }

    @Override
    public int hashCode() {
        return this.name.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof ChatClient)) {
            return false;
        }
        return this.name.equals(((ChatClient) o).getName());
    }

    @Override
    public String toString() {
        return "TcpProxyClient: [name=" + name + "] [room=" + room + "] [socket=" + socket + "]";
    }

    public void connect(Server server) {

    }

    public boolean isConnected() {
        return socket.isConnected();
    }

    public void incomingMessage(Message message) {
        try {
            sOutput.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Not the client part, need move to another class
    public void start() {
        (new Thread(this)).start();
    }

    public void run() {
        ChatMessage message = null;
        while (keepGoing) {
            try {
                message = (ChatMessage) sInput.readObject();
                processMessage(message);
            } catch (IOException e) {
                e.printStackTrace();
                server.removeClient(this);
                keepGoing = false;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                server.removeClient(this);
                keepGoing = false;
            }
        }

        close();
    }

    private void processMessage(Message message) {
        if (message.getType() == MessageType.LOGOUT) {
            server.removeClient(this);
            keepGoing = false;
            return;
        }
        server.processMessage(message, this);
    }

    private void close() {
        try {
            if (sOutput != null)
                sOutput.close();
        } catch (Exception e) {
        }

        try {
            if (sInput != null)
                sInput.close();
        } catch (Exception e) {
        }
        try {
            if (socket != null)
                socket.close();
        } catch (Exception e) {
        }
    }

    public void addListener(ClientListener listener) {
        // TODO Auto-generated method stub

    }
}
